package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
	@Autowired
	private SetmealService setmealService;
	@Autowired
	private RedisTemplate redisTemplate;
	@GetMapping("/{id}")
	@ApiOperation("根据id查询套餐")
	public Result<SetmealVO>getSetmealById(@PathVariable Long id){
		return Result.success(setmealService.getSetMealById(id));
	}
	@PostMapping("")
	public Result add(@RequestBody SetmealDTO setmealDTO){
		setmealService.add(setmealDTO);
		return Result.success();
	}
	@GetMapping("/page")
	public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
		return Result.success(setmealService.pageQuery(setmealPageQueryDTO));
	}
	@PutMapping("")
	public Result update(@RequestBody SetmealDTO setmealDTO){
		setmealService.updateWithDishes(setmealDTO);
		String key = "dish_*";
		cleanCache(key);
		return Result.success();
	}
	@DeleteMapping("")
	public Result delete(@RequestParam("ids") List<Long> ids){
		setmealService.deleteByIds(ids);
		String key = "dish_*";
		cleanCache(key);
		return Result.success();
	}
	//缓存清除
	private void cleanCache(String pattern){
		Set keys = redisTemplate.keys(pattern);
		redisTemplate.delete(keys);
	}
}
